package com.group2.view;
import static model.netBase.IP;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import model.*;
public class BankTopUpFrame extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JPasswordField textField_2;
	private JTextField textField_3;
	private JComboBox comboBox;
	private static String studentId=null;
	private static Account MyAccount=null;
	private JLabel lable1;
	private JLabel lable2;

	/**
	 * Create the panel.
	 */
	public BankTopUpFrame(String id,Account mine,JLabel l1,JLabel l2) {
		studentId=id;
		MyAccount=mine;
		lable1=l1;
		lable2=l2;
		setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JLabel lblNewLabel = new JLabel("姓名：");
		
		JLabel lblNewLabel_1 = new JLabel("一卡通号：");
		
		JLabel lblNewLabel_2 = new JLabel("充值方式：");
		
		JLabel lblNewLabel_3 = new JLabel("充值金额：");
		
		JLabel lblNewLabel_4 = new JLabel("密码：");
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField.setText(studentId);
		textField_2 = new JPasswordField();
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setEditable(false);
		textField_3.setColumns(10);
		textField_3.setText(MyAccount.getAccountName());
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"微信", "支付宝", "银行卡"}));
		
		JButton btnNewButton = new JButton("确认充值");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					TopUpActionPerformed(e);
				} catch (IOException | ClassNotFoundException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(102)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addGap(18)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel_2)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(comboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblNewLabel_3)
									.addComponent(lblNewLabel_4))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
									.addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)))
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblNewLabel_1)
									.addComponent(lblNewLabel))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(textField_3, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
									.addComponent(textField, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)))))
					.addContainerGap(107, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(26, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}
	/**
	 * 重置事件处理
	 * @param evt
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		this.resetValue();
	}
	/**
	 * 清空内容（重置）
	 */
	private void resetValue() {
		this.textField_1.setText("");
		this.textField_2.setText("");
		this.comboBox.setSelectedIndex(0);
	}

	/**
	 * 确认充值事件处理
	 * @param e
	 */
	private void TopUpActionPerformed( ActionEvent e) throws IOException, ClassNotFoundException {
		String money=textField_1.getText();//金额
		String password=textField_2.getText();//密码
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("63" + '\n'+studentId+'\n'+password+'\n'+money+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		BufferedReader br=new BufferedReader(new InputStreamReader(is));
		String str=br.readLine();
		System.out.println("传输结束");
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
		if (str.equals("true")) {
			JOptionPane.showMessageDialog(this, "充值成功!");
			refresh();
			lable1.setText("当前一卡通余额："+MyAccount.getAccountBalance());
		} else {
			JOptionPane.showMessageDialog(this, "充值失败!");
		}
	}
	public void refresh() throws IOException, ClassNotFoundException {
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("61" + '\n'+studentId+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream in = new ObjectInputStream(is);
		Object obj = in.readObject();
		System.out.println("传输结束");
		MyAccount= (Account) obj;
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
	}
}
